Skip to main content

Single Tensor Summation using Einsum

This is an example of how to properly use the einsum function to compute the summation of a single tensor.

from csdl_om import Simulatorimport numpy as npfrom csdl import Modelimport csdl

class ExampleTensorSummation(Model):
    def define(self):        # Shape of Tensor        shape3 = (2, 4, 3)        c = np.arange(24).reshape(shape3)
        # Declaring tensor        tens = self.declare_variable('c', val=c)
        # Summation of all the entries of a tensor        self.register_output(            'einsum_summ2',            csdl.einsum_new_api(                tens,                operation=[(33, 66, 99)],            ))

sim = Simulator(ExampleTensorSummation())sim.run()
print('c', sim['c'].shape)print(sim['c'])print('einsum_summ2', sim['einsum_summ2'].shape)print(sim['einsum_summ2'])
[[[ 0.  1.  2.]  [ 3.  4.  5.]  [ 6.  7.  8.]  [ 9. 10. 11.]]
 [[12. 13. 14.]  [15. 16. 17.]  [18. 19. 20.]  [21. 22. 23.]]]einsum_summ2 (1,)[276.]